import axios from "axios";
import {ElMessage} from "element-plus";

let request = axios.create({
    baseURL: '/api',
    timeout: 5000,
});

//请求拦截器
request.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

//响应拦截器
request.interceptors.response.use(
    (response) => {
        //响应拦截器成功的回调
        return response.data;
    }, (error) => {
        let status = error.response.status;
        console.log(error)
        switch (status) {
            case 404:
                ElMessage({
                    message: error.message,
                    type: 'error',
                });
                break;
            case 500 | 501 | 502 | 503 | 504 | 504:
                ElMessage({
                    message: '服务器内部异常',
                    type: 'error',
                });
                break;
            case  401:
                ElMessage({
                    message: '参数有误',
                    type: 'error',
                });
                break;
        }
        //处理http网络错误
        return Promise.reject(new Error(error.message))
    }
);

//对外暴露axios
export default request;
